home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 24 aspnet applications / aspnetapplications / errorloggermodule.vb < prev    next >
Encoding:
Text File  |  2002-03-18  |  1.9 KB  |  53 lines

  1. '  this is a custom HTTP Module
  2. ' it is enabled by the following lines in Web.Config
  3.  
  4. '<httpModules>
  5. '    <add name="ErrorLogger" type="AspnetApplications.ErrorLoggerModule,AspnetApplications" />
  6. '</httpModules>  
  7.  
  8. Public Class ErrorLoggerModule
  9.     Implements IHttpModule
  10.  
  11.     ' The Application object.
  12.     Dim WithEvents Application As HttpApplication
  13.  
  14.     ' This is where we store all error messages since the application was started
  15.     Public Shared ErrorMessages As New ArrayList()
  16.  
  17.     ' this method is called by ASP.NET when the application starts.
  18.  
  19.     Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
  20.         ' Store the Application object in a local variable.
  21.         Application = context
  22.     End Sub
  23.  
  24.     ' this method is called by ASP.NET when the application is about to end
  25.  
  26.     Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
  27.         ' Nothing important to do in this case.
  28.         Application = Nothing
  29.     End Sub
  30.  
  31.     ' the current HTTP module traps the Application_Error event
  32.     ' it prepares a detailed report of the error and appends it to the 
  33.     ' ErrorMessages arraylist object.
  34.  
  35.     Private Sub Application_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Application.Error
  36.         ' prepare details about the error.
  37.         Dim msg As String
  38.         msg &= "<b>An exception has occurred at</b> " & Now.ToString & "<BR />"
  39.         msg &= "<b>URL</b> = " & Application.Request.Path & "<BR />"
  40.         msg &= "<b>QueryString</b> = " & Application.Request.QueryString.ToString & "<BR /><BR />"
  41.  
  42.         ' Append details about the error, but convert CR-LF pairs.
  43.         msg &= Application.Server.GetLastError.ToString.Replace(ControlChars.CrLf, "<BR />")
  44.  
  45.         ' prepend to the collection of error messages
  46.         SyncLock ErrorMessages.SyncRoot
  47.             ErrorMessages.Insert(0, msg)
  48.         End SyncLock
  49.  
  50.     End Sub
  51.  
  52. End Class
  53.